
from AppStoreConnect.AppStoreConnectAPI import AppStoreConnectAPI


if __name__ == "__main__":
    print("hello world!")
    
    asc = AppStoreConnectAPI(api_key_path= "AuthKey_sales_and_report_key_S6BQ948292.p8", api_issuer_id= "69a6de81-f534-47e3-e053-5b8c7c11a4d1", api_key_id= "S6BQ948292")
    
    asc.get_sales_report("92612851", "2024-01-01")

    
#     # App Store Connect API URL for sales reports
#     url = "https://api.appstoreconnect.apple.com/v1/salesReports"
    
#     # Load the private key
#     with open('AuthKey_sales_and_report_key_S6BQ948292.p8', 'rb') as key_file:
#         private_key = serialization.load_pem_private_key(
#             key_file.read(),
#             password=None,
#             backend=default_backend()
#         )

#     # JWT headers
#     jwt_headers = {
#         "alg": "ES256",
#         "kid": "S6BQ948292",
#         "typ": "JWT"
#     }

#     # JWT payload
#     payload = {
#         "iss": "69a6de81-f534-47e3-e053-5b8c7c11a4d1",
#         "iat": int(time.time()),
#         "exp": int(time.time()) + 60,  # JWT is valid for 1 hour
#         "aud": "appstoreconnect-v1"
#     }

#     # Generate the JWT
#     token = jwt.encode(payload, private_key, algorithm='ES256', headers=jwt_headers)

#     # Headers for the request
#     headers = {
#         'Authorization': f'Bearer {token}',
#         'Content-Type': 'application/json'
#     }
        

#     # Parameters for the request (modify as needed)
#     params = {
#         "filter[frequency]": "DAILY",
#         "filter[reportType]": "SALES",
#         "filter[reportSubType]": "SUMMARY",
#         "filter[vendorNumber]": "92612851",
#         "filter[reportDate]": "2024-01-01"
#     }

#     # Send the request
#     response = requests.get(url, headers=headers, params=params)

#    # Check if the request was successful
# if response.status_code == 200:
#     # Check if the response is a zip file
#     if response.headers['Content-Type'] == 'application/a-gzip':
#         # The sales report is returned as a gzip file, so we need to ungzip it
#         g = gzip.GzipFile(fileobj=io.BytesIO(response.content))

#         # 如果没有reports目录，则创建一个
#         if not os.path.exists("reports"):
#             os.makedirs("reports"„´)

#         # Write the ungziped file to disk
#         open("reports/report.txt", 'wb').write(g.read())
#     else:
#         print(f"Unexpected content type: {response.headers['Content-Type']}")
# else:
#     print(f"Failed to get sales report: {response.status_code}, {response.text}")
    